package com.richman.view;

import java.awt.BorderLayout;
import java.awt.Canvas;

import javax.swing.JPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.richman.controller.BetClient;


public class SpecialBrowserPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -29817295377785602L;
	
	
    //用来搭载shell的canvas  
    private Canvas canvas = null;  
    //控制SWT组件的子线程  
    private DisplayThreadSpe displayThread = null;  
    
    Browser browser = null;
    
    String showText = "";
    
    private Combo combo = null;
    
   // private String[] accounts = {"账号1","账号2"};
      
    public SpecialBrowserPanel()  
    {  
        displayThread = new DisplayThreadSpe();  
        displayThread.start();  
        canvas = new Canvas();  
        setLayout(new BorderLayout());  
        add(canvas, BorderLayout.CENTER);  
    }  
      
    //改变panel呈现的样式  
    public void addNotify()   
    {  
        super.addNotify();  
        Display dis=displayThread.getDisplay();  
        //syncExec的函数作用是让dis所在线程视自己情况，找机会执行后面代码  
        dis.syncExec( new Runnable() {  
            //此处添加SWT界面的代码  
            public void run() {  
            Shell shell = SWT_AWT.new_Shell(displayThread.getDisplay(), canvas );  
            shell.setSize(1000, 900);  
              
          
            Button historyButton=new Button(shell,SWT.BORDER);   
            historyButton.setBounds(5,5,100,25);          
            historyButton.setText("账户历史");   
            
            
            
            
            Button statusButton=new Button(shell,SWT.BORDER);   
            statusButton.setBounds(200,5,100,25);          
            statusButton.setText("加载交易状况");   
            

            

            Button testButton=new Button(shell,SWT.BORDER);   
            testButton.setBounds(400,5,100,25);          
            testButton.setText("显示交易状况");   
            
            
            browser=new Browser(shell,SWT.FILL);  
            
//            System.out.println(System.getProperty("org.eclipse.swt.browser.XULRunnerPath"));
//            
//            browser=new Browser(shell,SWT.MOZILLA);   

            browser.setBounds(5,30,1000,700);   
              
            statusButton.addListener(SWT.Selection, new Listener()   
            {   
                public void handleEvent(Event event)   
                {   
//                	browser.setUrl(DaShuiClient.betCtrMng.hgmanager.getSpecialAccountShowTodayRecURL());
//                	browser.refresh();
                }   
            });   
            
            
            testButton.addListener(SWT.Selection, new Listener()   
            {   
                public void handleEvent(Event event)   
                {

//                	browser.execute("top.uid='" + DaShuiClient.betCtrMng.hgmanager.getSpecialAccountUser_id() + "'");
//                	browser.execute("javascript:reload_var()");

                }
                
            });
  
            

            
            
            
            
            historyButton.addListener(SWT.Selection, new Listener()   
            {   
                public void handleEvent(Event event)   
                {   
  
                	//browser.setUrl(DaShuiClient.betCtrMng.hgmanager.getSpecialAccountHistoryRecURL());
                	
                }   
            });   
            
            
            
            
            
            
            
            
           }  
          } );  
    }  
      
    public class DisplayThreadSpe extends Thread{  
        private Display display;  
        Object lock = new Object();  
          
        public void run(){  
            //添加同步，DisplayThread和SWTPanel所在不同线程，访问同一个display对象，  
            //SWTPanel需要等DisplayThread创建display对象后才能引用，所以此处要添加同步  
            synchronized (lock) {  
                display = Display.getDefault();  
                lock.notifyAll();  
            }  
            swtEventLoop();  
        }  
          
        private void swtEventLoop(){  
            while(true)  
            {  
                if(!display.readAndDispatch())  
                {  
                    display.sleep();  
                }  
            }  
        }  
          
        public Display getDisplay() {  
            synchronized (lock) {  
                while (display == null)   
                {  
                    try {  
                        lock.wait();  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
                return display;  
            }  
        }  
    }  
    
    
    
    public void setText(String html){
    	
    	showText = html;

    }
    
    


	

}
